package cc.rengu.igas.channel.wechat.core.service.trans;


import cc.rengu.igas.channel.wechat.common.constant.WechatParamConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatTreeNodeConstant;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.wechat.core.service.base.MchntService;
import cc.rengu.igas.channel.wechat.facade.bean.MchntInfoBean;
import cc.rengu.igas.channel.wechat.facade.request.WechatQueryMerchantRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatQueryMerchantResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;


/**
 * 商户信息查询
 */
public class WechatQueryMerchantService extends MchntService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        WechatQueryMerchantRequest queryMerchantRequest = new WechatQueryMerchantRequest();
        ConvertUtil.convertOutput(queryMerchantRequest);
        return queryMerchantRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;

        WechatQueryMerchantResponse queryMerchantResponse = new WechatQueryMerchantResponse();
        WechatQueryMerchantRequest queryMerchantRequest = (WechatQueryMerchantRequest) request;
        DstChannelCfg dstChannelCfg = (DstChannelCfg) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_CFG, DstChannelCfg.class);
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);

        ConvertUtil.convertInput(queryMerchantRequest);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, queryMerchantRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_CHANNEL_NO, dstChannelCfg.getChannelNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_MCHNT_NO, dstChannelCfg.getProviderMchntNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_APP_ID, dstChannelCfg.getAppId());
        if (queryMerchantRequest.getMchntNo() != null) {
            xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.MER_REMARK, queryMerchantRequest.getMchntNo());
        }
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (xmlTreeUtil.isExist(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE)) {
            xmlTreeUtil.deleteXmlTreeNode(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE);
        }
        if (0 != iReturnCode && 98 != iReturnCode) {
            rglog.error("调用微信服<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        } else if (98 == iReturnCode) {
            rglog.error("调用微信服<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(queryMerchantResponse);
        String resultCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RESULT_CODE);
        String returnCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_CODE);
        String returnMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_MSG);
        String errCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_ERR_CODE);
        String errMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_ERR_MSG);
        if (!WechatParamConstant.STRING_SUCCESS.equals(resultCode)) {
            queryMerchantResponse.setRespCode((null == errCode) ? returnCode : errCode);
            queryMerchantResponse.setRespDesc((null == errMsg) ? returnMsg : errMsg);
            rglog.error("调用第三方服务失败,respCode=[{}],respDesc=[{}]", queryMerchantResponse.getRespCode(), queryMerchantResponse.getRespDesc());
        } else {
            queryMerchantResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            queryMerchantResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            String merInfo = xmlTreeUtil.getXmlTreeStringValue("merInfo");
            if (merInfo != null) {
                JSONArray jsonObject = JSON.parseArray(merInfo);
                if (jsonObject.size() > 0) {
                    List<MchntInfoBean> mchntInfoBeanList = new ArrayList<>();
                    for (int i = 0; i < jsonObject.size(); i++) {
                        MchntInfoBean mchntInfoBean = new MchntInfoBean();
                        if (jsonObject.getJSONObject(i).get("business") != null) {
                            mchntInfoBean.setBusiness((String) jsonObject.getJSONObject(i).get("business"));
                        }
                        if (jsonObject.getJSONObject(i).get("business") != null) {
                            mchntInfoBean.setChannelId((String) jsonObject.getJSONObject(i).get("channel_id"));
                        }
                        if (jsonObject.getJSONObject(i).get("business") != null) {
                            mchntInfoBean.setContact((String) jsonObject.getJSONObject(i).get("contact"));
                        }
                        if (jsonObject.getJSONObject(i).get("business") != null) {
                            mchntInfoBean.setChnlMerName((String) jsonObject.getJSONObject(i).get("channel_name"));
                        }
                        if (jsonObject.getJSONObject(i).get("business") != null) {
                            mchntInfoBean.setThirdMchntNo((String) jsonObject.getJSONObject(i).get("mch_id"));
                        }
                        if (jsonObject.getJSONObject(i).get("business") != null) {
                            mchntInfoBean.setServicePhone((String) jsonObject.getJSONObject(i).get("servicePhone"));
                        }
                        if (jsonObject.getJSONObject(i).get("business") != null) {
                            mchntInfoBean.setMchntName((String) jsonObject.getJSONObject(i).get("merchant_name"));
                        }
                        if (jsonObject.getJSONObject(i).get("business") != null) {
                            mchntInfoBean.setMchntAbbr((String) jsonObject.getJSONObject(i).get("merchant_shortname"));
                        }
                        mchntInfoBeanList.add(mchntInfoBean);
                    }
                    queryMerchantResponse.setMchntInfoBeanList(mchntInfoBeanList);
                }
            }
        }
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(queryMerchantResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        WechatQueryMerchantResponse queryMerchantResponse = (WechatQueryMerchantResponse) response.getResult();
        ConvertUtil.convertInput(queryMerchantResponse);
    }


}
